/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package parser;

import gui.CharacterWindow;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import javax.swing.JPanel;
import jpen.PButtonEvent;
import jpen.PKindEvent;
import jpen.PLevel;
import jpen.PLevelEvent;
import jpen.PScrollEvent;
import jpen.PenManager;
import jpen.event.PenListener;

/**
 *
 * @author darek
 */
public class TabletConnector implements PenListener{
    private Graphics2D g=null;
    private JPanel panel=null;
    private float prevX=-1, prevY=-1;
    private long prevTime;
    @SuppressWarnings("unused")
	private float speed, dist;
    private ArrayList<Integer[][]> list=null;
    private ArrayList<Double> pressureList=null;
    private int count=0;
    private CharacterWindow cw=null;
    private double factor=0.05;
    private boolean goingUp=false;
    private boolean goingDown=false;
    private boolean newLetter=false;

    public TabletConnector(JPanel panel, CharacterWindow cw){
        this.panel=panel;
        this.g=(Graphics2D)this.panel.getGraphics();
        g.setStroke(new BasicStroke(2));
        this.cw=cw;
        this.g.setColor(Color.BLUE);
        PenManager pm=new PenManager(panel);
        pm.pen.addListener(this);
        list=new ArrayList<Integer[][]>();
        pressureList=new ArrayList<Double>();
    }

    public void penKindEvent(PKindEvent pke) {
    }

    public void penLevelEvent(PLevelEvent ple) {
        if(!ple.isMovement())return;
        float x=ple.pen.getLevelValue(PLevel.Type.X);
        float y=ple.pen.getLevelValue(PLevel.Type.Y);
        double pressure=ple.pen.getLevelValue(PLevel.Type.PRESSURE);
        if(pressure>0.0){
            cw.pressure.setText(""+pressure);
            double min=new Double(cw.min.getText());
            double max=new Double(cw.max.getText());
            if(pressure<min)cw.min.setText(""+pressure);
            if(pressure>max)cw.max.setText(""+pressure);
            if(prevX==-1){
                g.fillOval((int)x, (int)y, 2, 2);
            }else{
                g.drawLine((int)x, (int)y, (int)prevX, (int)prevY);

                Integer[][] temp={{(int)x,(int)y,(int)prevX,(int)prevY}};

                list.add(temp);
                pressureList.add(pressure);
            }
            if(!goingUp&&isGoingUp(list)){
                goingUp=true;
            }
            if(!goingDown&&goingUp&&isGoingDown(list)){
                goingDown=true;
            }
            if(goingUp&&goingDown&&isGoingUp(list)){
                goingUp=false;
                goingDown=false;
                newLetter=true;
            }
            if(ple.getTime()>prevTime){
                speed=dist/(ple.getTime()-prevTime);
                prevTime=ple.getTime();
                dist=0;
            }else{
                dist+=Math.sqrt(Math.pow(y-prevY, 2)+Math.pow(x-prevX, 2));
            }
//            double[] v={x,y,pressure,speed};
//        }else if(pressure==0.0){
        }
        if(pressure < this.factor || newLetter)
        {
            if(list.size()>0){
                cw.newCharacter(list,pressureList);
                if((count++)%2==0){
                    g.setColor(Color.RED);
                }else{
                    g.setColor(Color.GREEN);
                }
            }

            for(Integer[][] c: list){
                g.drawLine(c[0][0], c[0][1], c[0][2], c[0][3]);
            }

            list.clear();
            pressureList.clear();
            g.setColor(Color.BLUE);
            goingUp=false;
            goingDown=false;
            newLetter=false;
        }
        prevX=x;
        prevY=y;
    }

    public void penButtonEvent(PButtonEvent pbe) {
    }

    public void penScrollEvent(PScrollEvent pse) {
    }

    public void penTock(long l) {
    }

    public void setFactor(double factor){
        this.factor=factor;
    }

    private boolean isGoingUp(ArrayList<Integer[][]> list) {
        int size=list.size();
        if(list.size()<3)return false;
        float y=list.get(size-1)[0][1];
        if(y<list.get(size-2)[0][1] && y<list.get(size-3)[0][1]){
            return true;
        }
        return false;
    }

    private boolean isGoingDown(ArrayList<Integer[][]> list) {
        int size=list.size();
        if(list.size()<3)return false;
        float y=list.get(size-1)[0][1];
        if(y>list.get(size-2)[0][1] && y>list.get(size-3)[0][1])return true;
        return false;
    }
}
